package graficos;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class Grafico extends HttpServlet{

	private static final long serialVersionUID = -7857432081931388551L;
	
	protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
			response.setContentType("image/png");
			OutputStream output = response.getOutputStream();
			BufferedImage image = criarGrafico(criarDataSet()).createBufferedImage(250, 250);
			ImageIO.write(image, "png", output);
			output.close();
			}
	
	private PieDataset criarDataSet() {
		NumberFormat formatador = DecimalFormat.getNumberInstance(new Locale("pt", "BR"));
		Float a = new Float(10);
		Float b = new Float(5);
		Float c = new Float(1);
		DefaultPieDataset dataSet = new DefaultPieDataset();
		dataSet.setValue("a [" + formatador.format(a) + "]", a);
		dataSet.setValue("b [" + formatador.format(b) + "]", b);
		dataSet.setValue("c [" + formatador.format(c) + "]", c);
		return dataSet;
		}
	
	private JFreeChart criarGrafico(PieDataset dataSet) {
		JFreeChart chart = ChartFactory.createPieChart3D("Receitas x Despesas",dataSet, false, true, false);
		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setBackgroundPaint(Color.WHITE);
		plot.setStartAngle(290);
		plot.setForegroundAlpha(0.5f);
		return chart;
	}

}
